package tk.djcrazy.instantmessage.action;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;

import tk.djcrazy.instantmessage.domain.FriendShip;
import tk.djcrazy.instantmessage.domain.UserEntity;
import tk.djcrazy.instantmessage.service.IUserService;

import com.opensymphony.xwork2.ActionSupport;

public class ListFriendAction extends ActionSupport implements SessionAware {
 	private static final long serialVersionUID = -8718104043569651L;

	private Map<String, Object> session;
 	private UserEntity entity;
 	private String dataAutoComplete;
 	@Resource
 	private IUserService userService;
 
	@Override
	public String execute() {
		try {
		entity = userService.findById((Integer)session.get("uid")); 
		dataAutoComplete = "";
		for (FriendShip ship : entity.getFriends()) {
			dataAutoComplete+=ship.getSlaveUser().getUserName()+"("+ship.getSlaveUser().getId()+"),";
		}
		dataAutoComplete = dataAutoComplete.substring(0, dataAutoComplete.length());
		dataAutoComplete = "[ "+dataAutoComplete+ " ]";
 		return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public UserEntity getEntity() {
		return entity;
	}

	public void setEntity(UserEntity entity) {
		this.entity = entity;
	}

	public IUserService  getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public String getDataAutoComplete() {
		return dataAutoComplete;
	}

	public void setDataAutoComplete(String dataAutoComplete) {
		this.dataAutoComplete = dataAutoComplete;
	}

 }
